package org.jeecg.glr.api.controller;


import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.glr.entity.GlrVideoBdRecognize;
import org.jeecg.glr.service.IGlrVideoBdRecognizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.glr.service.IGlrVideoBdService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.util.Iterator;

/**
 * 外部调用
 * 摄像头上传抓拍的人脸图片
 */
@Slf4j
@Controller
@RequestMapping(value = "/apiGlr/BdFace")
public class ApiBdFaceController {
    @Value(value = "${jeecg.path.facehomedirectory}")
    private String facehomedirectory;

    @Autowired
    private IGlrVideoBdService glrVideoBdService;

    @RequestMapping("/fileFace")
    @ResponseBody
    public Result<?> fileface(HttpServletRequest request, HttpServletResponse response, String code) throws  Exception{
        String filePath =facehomedirectory;
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        //获取multiRequest 中所有的文件名
        Iterator iter=multipartRequest.getFileNames();
        while(iter.hasNext()){
            //一次遍历所有文件
            MultipartFile file=multipartRequest.getFile(iter.next().toString());
            String getOriginalFilename=file.getOriginalFilename();
            if(getOriginalFilename.indexOf("backgroundImage")!=-1){
                continue;
            }
            File savefile = new File(filePath+File.separator+code+File.separator+file.getName()+"_"+getOriginalFilename);
            FileCopyUtils.copy(file.getBytes(), savefile);

            GlrVideoBdRecognize entity =new GlrVideoBdRecognize();
            entity.setBaiduServerCode("101");
            entity.setCatalog("/"+code+File.separator+file.getName()+"_"+getOriginalFilename);
            entity.setEquipmentSerial(code);
            entity.setTakePhotoTimeString(file.getName().substring(0,19));
            BASE64Encoder encoder = new BASE64Encoder();
            glrVideoBdService.doPhotoMatch(entity,encoder.encode(file.getBytes()));
        }
        return Result.ok("ok");
    }


}
